﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using EmoneyWeiXin.Filter;
using EmoneyWeiXinEntity.TxzEntity;

namespace EmoneyWeiXin.Controllers
{
    public class AccountController : Controller
    {

        [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            return View();
        }

        string LoginUrl = System.Configuration.ConfigurationManager.AppSettings["LoginUrl"];
        string ValidateClientTicketApiUrl = System.Configuration.ConfigurationManager.AppSettings["ValidateClientTicketApiUrl"];
        string GetUserInfoApiUrl = System.Configuration.ConfigurationManager.AppSettings["GetUserInfoApiUrl"];
        string md5key = System.Configuration.ConfigurationManager.AppSettings["md5key"];
        /// <summary>
        /// 头部
        /// </summary>
        /// <returns></returns>
        public ActionResult Top()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null && !string.IsNullOrEmpty(us.Username))
            {
                Response.Redirect(StringTools.GetTrendsFile("account/Index"));
            }
            else
            {
                //return Redirect(LoginUrl);
            }
            return View();//Content("");
        }


        public ActionResult BackRedirect()
        {
            //1.获取ticket
            string ticket = Request["ticket"];
            if (!string.IsNullOrEmpty(ticket))
            {
                //2.获取票据对应的登录信息
                string json = string.Empty;
                string url = WebApiHelper.CreateUrl(string.Format(ValidateClientTicketApiUrl, ticket), md5key);
                json = NetHelper.LoadData(url, null);
                ApiResult ar = JsonTools.ToObject<ApiResult>(json);
                if (ar.RetCode == "0")
                {
                    if (ar.Message != null)
                    {
                        //3.反序列化票据的登录信息
                        TicketInfo tic = JsonTools.ToObject<TicketInfo>(ar.Message.ToString());

                        //4.获取用户实体对象信息
                        url = WebApiHelper.CreateUrl(string.Format(GetUserInfoApiUrl, tic.UserID), md5key);
                        json = NetHelper.LoadData(url, null);
                        ApiResult ar1 = JsonTools.ToObject<ApiResult>(json);

                        if (ar1.RetCode == "0")
                        {
                            if (ar1.Message != null)
                            {
                                UserInfo userInfo = JsonTools.ToObject<UserInfo>(ar1.Message.ToString());
                                UserBLL userBll = new UserBLL();
                                UserEntity entity = userBll.GetUserEntityByTxzId(userInfo.ID);
                                if (entity != null)
                                {
                                    LoginUserInfo.AddLoginUserSave(entity);
                                }
                                else {
                                    Response.Redirect(StringTools.GetTrendsFile("account/Login"));
                                }
                               
                            }
                            //5.跳转到应用系统主页
                            Response.Redirect(StringTools.GetTrendsFile("account/Index"));
                        }
                        else
                        {
                            return Redirect(LoginUrl);
                        }
                    }
                    else
                    {
                        return Redirect(LoginUrl);
                    }
                }
                else
                {
                    return Redirect(LoginUrl);
                }
            }

            return View();
        }


        /// <summary>
        /// 左边列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Left()
        {

            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            LeftMemuBLL bll = new LeftMemuBLL();
            // ViewBag.ListLeftMemu = bll.getListLiftMemuAll();
            ViewBag.ListLeftMemu = bll.getListLiftMemuAll(uid);

            return View();
        }

        public ActionResult Right()
        {
            return View();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Main()
        {
            return View();
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOut()
        {
            LoginUserInfo.deleteUserInfo();
            Response.Redirect(StringTools.GetTrendsFile("account/Login"));
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public string RegisterUser()
        {
            string txtUserName = HttpUtility.UrlDecode(Request.Form["txtUserName"]);
            string txtUserPwd = HttpUtility.UrlDecode(Request.Form["txtUserPwd"]);

            if (string.IsNullOrEmpty(txtUserName))
            {
                return "-1";
            }
            if (string.IsNullOrEmpty(txtUserPwd))
            {
                return "-2";
            }

            if (txtUserName.Contains("'") || txtUserName.Contains("-") || txtUserName.Contains("'") || txtUserName.Contains("-"))
            {
                return "-3";
            }
            UserBLL bll = new UserBLL();
            var queryUser = bll.getUserAll(txtUserName, txtUserPwd);
            if (queryUser != null && queryUser.Count == 1)
            {
                LoginUserInfo.AddLoginUserSave(queryUser[0]);
                return "1";
            }
            return "0";
        }
    }
}
